package com.xggstudio.crm.ui.mvp.v;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.xggstudio.crm.R;
import com.xggstudio.crm.base.BaseActivity;
import com.xggstudio.crm.base.Constant;
import com.xggstudio.crm.ui.mvp.m.CusInfo;
import com.xw.repo.BubbleSeekBar;

import java.util.List;

public class AddAssistantActivity extends BaseActivity implements View.OnClickListener {

    private Toolbar toolbar;

    private TextView bartext;

    private String title;

    private TextView name;

    private BubbleSeekBar ratio;

    private int resultCode;

    private List<CusInfo.ResultBean.AdminUserBean> users;

    CusInfo.ResultBean.AdminUserBean info;

    @Override
    protected int getLayout() {
        return R.layout.activity_add_assist;
    }

    @Override
    protected void init(Bundle savedInstanceState) {

        initView();

        getIntentData();
    }

    @SuppressLint("RestrictedApi")
    private void initView(){

        bartext = (TextView) $(R.id.bar_text);
        toolbar = (Toolbar) $(R.id.toolbar);

        name = (TextView) $(R.id.name);
        ratio = (BubbleSeekBar) $(R.id.ratio);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        name.setOnClickListener(this);

    }

    private void getIntentData(){

        if(getIntent() != null){
            title = getIntent().getStringExtra(Constant.CUS_NAME);
            users = (List<CusInfo.ResultBean.AdminUserBean>) getIntent().getSerializableExtra(Constant.CUS_CONTENT);

            resultCode = getIntent().getIntExtra(Constant.CUS_RESULS,0);

            bartext.setText(title);

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add_cus,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.menu_add:
                saveFinish();
                break;
            case android.R.id.home:
                finish();
                break;
        }

        return super.onOptionsItemSelected(item);
    }


    private void showAssistman(){

        final String[] arrays = new String[users.size() + 1];

        arrays[0] = "无";
        for (int i = 0; i < users.size(); i++) {

            CusInfo.ResultBean.AdminUserBean temp = users.get(i);
            arrays[i+1] = temp.getName();
        }

        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("选择");
        alert.setItems(arrays, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                name.setText(arrays[i]);
                if(i != 0) {
                    info = getUserid(arrays[i]);
                }
            }
        });

        alert.create().show();


    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.name) {
            showAssistman();
        }
    }

    private void saveFinish(){

        String s = name.getText().toString();

        if(TextUtils.isEmpty(s)){

            return;
        }

        if(s.equals("无")){
            finish();
            return;
        }

        int c = ratio.getProgress();

        info.setRatio(c);

        Intent intent = new Intent();
        intent.putExtra(Constant.CUS_NAME,title);
        intent.putExtra(Constant.CUS_CONTENT,info);

        setResult(resultCode,intent);

        finish();
    }


    private CusInfo.ResultBean.AdminUserBean getUserid(String name){
        if(users != null){
            for (int i = 0; i < users.size(); i++) {

                CusInfo.ResultBean.AdminUserBean temp = users.get(i);

                if(temp.getName().equals(name)){
                    return temp;
                }

            }
        }

        return null;
    }
}
